package com.example.libraryapp.fragment.category;

import android.content.Intent;
import android.view.View;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.libraryapp.Constant;
import com.example.libraryapp.R;
import com.example.libraryapp.activity.bookList.BookListActivity;
import com.example.libraryapp.adapter.CategoryAdapter;
import com.example.libraryapp.adapter.base.BaseAdapter;
import com.example.libraryapp.bean.BookType;
import com.example.libraryapp.bean.response.BaseResponse;
import com.example.libraryapp.network.base.BaseFragment;
import com.example.libraryapp.widget.CnToolbar;

import java.util.List;

/**
 * @author 伍凯铭
 * @since 2020/6/18
 */
public class CategoryFragment extends BaseFragment<CategoryContract.View,CategoryPresenterIml>
        implements CategoryContract.View, BaseAdapter.OnItemClickListener {

    private CategoryAdapter mCategoryAdapter;

    @Override
    public void onStart() {
        super.onStart();
        reload();
    }

    @Override
    protected void reload() {
        showLoading();
        mPresenter.getAllBookType();
    }

    @Override
    public void onItemClick(View view, int position) {
        BookType bookType = mCategoryAdapter.getItem(position);
        Intent intent = new Intent(getActivity(), BookListActivity.class);
        intent.putExtra(Constant.TYPE_DATA,bookType);
        getActivity().startActivity(intent);
    }

    @Override
    public void showAllBookType(BaseResponse<List<BookType>> response) {
        mCategoryAdapter.refreshData(response.getResult());
    }

    @Override
    protected void initView(View view) {
        RecyclerView recyclerView = view.findViewById(R.id.recycler_view);
        mCategoryAdapter = new CategoryAdapter(getActivity());
        mCategoryAdapter.setOnItemClickListener(this);
        recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 2));
        recyclerView.setAdapter(mCategoryAdapter);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_category;
    }

    @Override
    protected CategoryPresenterIml initPresenter() {
        return new CategoryPresenterIml();
    }

    @Override
    protected void initToolBar(CnToolbar toolbar) {
        toolbar.setTitle("分类");
    }
}
